//
//  BBAppDelegate.m
//  BabyThermometer
//
//  Created by CaiLei on 10/2/14.
//  Copyright (c) 2014 baohu. All rights reserved.
//

#import "BBAppDelegate.h"
#import "LeSideMenuViewController.h"
#import "BBMainViewController.h"
#import "BBSideViewController.h"
#import "BBBtDateUtil.h"
#import "BBTestRequest.h"
#import "BBAddBabyRequest.h"

#import "BBRegisterRequest.h"

@implementation BBAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:@"1119224079"];
    [BBGlobalSingleton setupCore];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    BBMainViewController *mainVC = [[BBMainViewController alloc] init];
    UINavigationController *mainNavi = [[UINavigationController alloc] initWithRootViewController:mainVC];
    BBSideViewController *sideVC = [[BBSideViewController alloc] init];

    LeSideMenuViewController *sideMenuViewController = [[LeSideMenuViewController alloc] initWithContentViewController:mainNavi menuViewController:sideVC];
    
    sideMenuViewController.menuOffsetX = 256.f;
    sideMenuViewController.bInnerPan = NO;
    sideMenuViewController.bOutterPan = NO;
    
    self.window.rootViewController = sideMenuViewController;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [[BBGlobalSingleton sharedInstance] loadFromDisk];
    
    [self forTestOnly];
    return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    [[BBGlobalSingleton sharedInstance] saveToDisk];
    [BBGlobalSingleton clearCore];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    if ([TencentOAuth HandleOpenURL:url]) {
        return YES;
    }
    
    if ([WeiboSDK handleOpenURL:url delegate:self]) {
        return YES;
    }
    
    return NO;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    if ([TencentOAuth HandleOpenURL:url]) {
        return YES;
    }
    
    if ([WeiboSDK handleOpenURL:url delegate:self]) {
        return YES;
    }
    
    return NO;
}

- (void)didReceiveWeiboRequest:(WBBaseRequest *)request {
    
}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
    
}

#pragma mark - test only
- (void)forTestOnly {
    [self testBTData];
}

- (void)testBTData {
    NSError *error = nil;
    NSData *data = [BBBtDateUtil dataFromHexString:@"0x 0e 0a 0b 0d 30 00" error:&error];
    if (!data) {
        NSLog(@"fail : %@", error);
    } else {
        NSLog(@"success : %@", data);
    }
    
    data = [BBBtDateUtil dataFromHexString:@"0x 91 63" error:&error];
    Bt_Data_Type type = [BBBtDateUtil dataTypeFor:data];
    
    data = [BBBtDateUtil dataFromHexString:@"0x 94" error:&error];
    type = [BBBtDateUtil dataTypeFor:data];
    
    data = [BBBtDateUtil dataFromHexString:@"0x 95" error:&error];
    type = [BBBtDateUtil dataTypeFor:data];
    
    data = [BBBtDateUtil dataFromHexString:@"0x 91" error:&error];
    type = [BBBtDateUtil dataTypeFor:data];
    
    data = [BBBtDateUtil dataFromHexString:@"0x 91 63 24" error:&error];
    type = [BBBtDateUtil dataTypeFor:data];
    
    data = [BBBtDateUtil dataFromHexString:@"0x 94 23" error:&error];
    type = [BBBtDateUtil dataTypeFor:data];
    
    CGFloat t;
    data = [BBBtDateUtil dataFromHexString:@"0x 91 01" error:&error];
    t = [BBBtDateUtil temparatureFromData:data];
    
    data = [BBBtDateUtil dataFromHexString:@"0x 91 22" error:&error];
    t = [BBBtDateUtil temparatureFromData:data];
    
    data = [BBBtDateUtil dataFromHexString:@"0x 91 50" error:&error];
    t = [BBBtDateUtil temparatureFromData:data];
    
    data = [BBBtDateUtil dataFromHexString:@"0x 91 51" error:&error];
    t = [BBBtDateUtil temparatureFromData:data];
    
    data = [BBBtDateUtil dataFromHexString:@"0x 91 63" error:&error];
    t = [BBBtDateUtil temparatureFromData:data];
    
    data = [BBBtDateUtil dataFromHexString:@"0x 91 64" error:&error];
    t = [BBBtDateUtil temparatureFromData:data];
    
    data = [BBBtDateUtil dataFromHexString:@"0x 91 ff" error:&error];
    t = [BBBtDateUtil temparatureFromData:data];
    
    data = [BBBtDateUtil dataWithDate:[NSDate date]];
}
@end
